resource "alicloud_slb" "main" {
  count                = var.number_of_instances == "" ? 1 : 0
  name                 = "${var.slb_name}-slb"
  internet_charge_type = var.slb_internet_charge_type
  address_type         = var.slb_address_type
  vswitch_id           = var.slb_vswitch_id[count.index]
  specification        = var.slb_instance_type
  bandwidth            = var.slb_bandwidth
  tags                 = var.tags
}
resource "alicloud_slb_listener" "main" {
  count                = var.enable_slb_listener ? 1 : 0  
  load_balancer_id     = alicloud_slb.main.0.id
  backend_port         = var.slb_backend_port
  frontend_port        = var.slb_frontend_port
  protocol             = var.slb_protocol
  bandwidth            = var.slb_listen_bandwidth
  scheduler            = var.slb_scheduler

  // Health Check
  healthy_threshold     = var.slb_healthy_threshold
  unhealthy_threshold   = var.slb_unhealthy_threshold
  health_check_timeout  = var.slb_health_check_timeout
  health_check_interval = var.slb_health_check_interval

  // HTTP and HTTPS
  sticky_session      = var.slb_enable_sticky_session ? "on" : "off"
  sticky_session_type = var.slb_sticky_session_type
  cookie              = var.slb_cookie
  cookie_timeout      = var.slb_cookie_timeout
  health_check        = var.slb_enable_health_check ? "on" : "off"
  gzip                = var.slb_enable_gzip
  server_certificate_id = var.slb_server_certificate_id
  enable_http2        = var.slb_enable_http2
  tls_cipher_policy   = var.slb_tls_cipher_policy
  listener_forward    = var.slb_listener_forward

  x_forwarded_for {
    retrive_slb_ip    = var.retrive_slb_ip
    retrive_slb_id    = var.retrive_slb_id
    retrive_slb_proto = var.retrive_slb_proto
  }

  // TCP, HTTP and HTTPS
  health_check_connect_port = var.slb_health_check_connect_port == "" ? var.slb_backend_port : var.slb_health_check_connect_port
  health_check_domain       = var.slb_health_check_domain
  health_check_uri          = var.slb_health_check_uri
  health_check_http_code    = var.slb_health_check_http_code

  // TCP
  health_check_type = var.slb_health_check_type

  // TCP and UDP
  persistence_timeout = var.slb_persistence_timeout

  acl_status      = var.slb_acl_status
  acl_type        = var.slb_acl_type
  acl_id          = var.slb_acl_id == "" ? alicloud_slb_acl.main.id : var.slb_acl_id
  request_timeout = var.slb_acl_request_timeout
  idle_timeout    = var.slb_acl_idle_timeout
}
resource "alicloud_slb_acl" "main" {
  name                      = "${var.slb_name}-acl"
  ip_version                = var.slb_acl_ip_version
  dynamic "entry_list" {
    for_each                = var.slb_acl_entry_list
    content {
      comment               = lookup(entry_list.value, "comment", null)
      entry                 = entry_list.value.entry
    }
  }
}

resource "alicloud_slb_attachment" "main" {
  count                         = var.enable_slb_attachment ? 1 : 0
  load_balancer_id              = alicloud_slb.main.0.id
  instance_ids                  = var.slb_attach_instance_ids
}
#   entry_list Example:
#   slb_acl_entry_list            = [
#     {
#       entry   = "10.10.10.0/24"
#       comment = "first"
#     },
#     {
#       entry   = "168.10.10.0/24"
#       comment = "second"
#     },
#     {
#       entry   = "172.10.10.0/24"
#       comment = "third"
#     },
#   ]
